package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.dto.CompanyInfoDto;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.request.wx.CommonRequest;
import com.ocom.common.request.wx.CompanyInfoPageUpdateColorRequest;
import com.ocom.common.request.wx.CompanyInfoPageUpdateNameRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.CompoanyInfoPageOnShowResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.ColorVo;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.CompanyClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/CompanyInfoPage")
@RestController
public class CompoanyInfoPageController {

    private ArrayList<ColorVo> colorList=new ArrayList<ColorVo>(){{
        add(new ColorVo(1,"ff0000"));
        add(new ColorVo(2,"00ff00"));
        add(new ColorVo(3,"0000ff"));
        add(new ColorVo(4,"ffff00"));
        add(new ColorVo(5,"ff00ff"));
        add(new ColorVo(6,"00ffff"));
        add(new ColorVo(7,"ff0000"));
        add(new ColorVo(8,"ff0000"));
    }};

    @Autowired
    CompanyClientService companyClientService;

    @Autowired
    AccessClientService accessClientService;


    @PostMapping("onShow")
    public Result onShow(@Valid CommonRequest request){

        CompoanyInfoPageOnShowResponse compoanyInfoPageOnShowResponse=new CompoanyInfoPageOnShowResponse();
        Map<String, Object> pageFormat=new HashMap<>();
        pageFormat.put("pageTitle","账号信息+");
        pageFormat.put("lblNameCaption","修改单位名称");
        pageFormat.put("cmdNameCaption","选择卡片颜色");
        pageFormat.put("lblColor1Caption","选择卡片颜色");
        pageFormat.put("lblColor2Caption","当前颜色");
        pageFormat.put("cmdColorCaption","修改卡片颜色");
        compoanyInfoPageOnShowResponse.setPageFormat(pageFormat);


        //1.companyName、endDate
        Result result=companyClientService.getCompanyInfoById(request.getCompanyId());
        if(result.getCode()==200){
            EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(result.getData()), EntityCompanyInfo.class);
            compoanyInfoPageOnShowResponse.setCompanyName(entityCompanyInfo.getComName());
            compoanyInfoPageOnShowResponse.setColorNo(entityCompanyInfo.getColor());
        }

        compoanyInfoPageOnShowResponse.setSkinColor(colorList);
        return ResultUtil.success(compoanyInfoPageOnShowResponse);
    }


    @PostMapping("/updateName")
    public Result updateName(@Valid CompanyInfoPageUpdateNameRequest request){


        Result result = companyClientService.modifyName(request.getCompanyId(),request.getCompanyName());
        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }

        Map<String,Object> integerMap=new HashMap<>();
        integerMap.put("newName",request.getCompanyName());
        return ResultUtil.info(200,"您的账号已更名为:"+request.getCompanyName(),integerMap);
    }


    @PostMapping("/updateColor")
    public Result updateColor(@Valid CompanyInfoPageUpdateColorRequest request){
        Result result = companyClientService.modifyColor(request.getCompanyId(),request.getColorNo());
        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }
        Map<String,Object> integerMap=new HashMap<>();
        integerMap.put("colorNo",request.getColorNo());
        return ResultUtil.info(200,"卡片颜色已修改。",integerMap);
    }




}
